package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.EquipmentAcceptance;
import com.amsp.oa.oademo.entity.EquipmentAccessories;
import com.amsp.oa.oademo.mapper.EquipmentAcceptanceMapper;
import com.amsp.oa.oademo.mapper.EquipmentAccessoriesMapper;
import com.amsp.oa.oademo.service.EquipmentAcceptanceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.LocalDate;
import java.util.List;

@Service
public class EquipmentAcceptanceServiceImpl extends ServiceImpl<EquipmentAcceptanceMapper, EquipmentAcceptance>
        implements EquipmentAcceptanceService {

    @Autowired
    private EquipmentAccessoriesMapper accessoriesMapper;

    @Override
    public Page<EquipmentAcceptance> getPage(Integer current, Integer size, String equipmentName,
            String specificationModel, String equipmentNumber, String acceptancePerson,
            LocalDate startDate, LocalDate endDate) {

        LambdaQueryWrapper<EquipmentAcceptance> wrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (StringUtils.hasText(equipmentName)) {
            wrapper.like(EquipmentAcceptance::getEquipmentName, equipmentName);
        }

        if (StringUtils.hasText(specificationModel)) {
            wrapper.like(EquipmentAcceptance::getSpecificationModel, specificationModel);
        }

        if (StringUtils.hasText(equipmentNumber)) {
            wrapper.like(EquipmentAcceptance::getEquipmentNumber, equipmentNumber);
        }

        if (StringUtils.hasText(acceptancePerson)) {
            wrapper.like(EquipmentAcceptance::getAcceptancePerson, acceptancePerson);
        }

        // 日期范围查询
        if (startDate != null) {
            wrapper.ge(EquipmentAcceptance::getAcceptanceDate, startDate);
        }

        if (endDate != null) {
            wrapper.le(EquipmentAcceptance::getAcceptanceDate, endDate);
        }

        // 按创建时间降序排序
        wrapper.orderByDesc(EquipmentAcceptance::getCreateTime);

        return page(new Page<>(current, size), wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveWithAccessories(EquipmentAcceptance equipmentAcceptance) {
        LocalDateTime now = LocalDateTime.now();
        if (equipmentAcceptance.getId() != null) {
            // 更新
            equipmentAcceptance.setUpdateTime(now);
            // 删除原有附件
            LambdaQueryWrapper<EquipmentAccessories> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(EquipmentAccessories::getEquipmentId, equipmentAcceptance.getId());
            accessoriesMapper.delete(wrapper);
        } else {
            // 新增
            equipmentAcceptance.setCreateTime(now);
            equipmentAcceptance.setUpdateTime(now);
        }

        boolean success = this.saveOrUpdate(equipmentAcceptance);
        if (success && equipmentAcceptance.getAccessoriesList() != null) {
            List<EquipmentAccessories> accessoriesList = equipmentAcceptance.getAccessoriesList();
            for (EquipmentAccessories accessories : accessoriesList) {
                accessories.setEquipmentId(equipmentAcceptance.getId());
                accessories.setCreateTime(now);
                accessories.setUpdateTime(now);
                accessoriesMapper.insert(accessories);
            }
        }
        return success;
    }

    @Override
    public EquipmentAcceptance getDetailById(Long id) {
        EquipmentAcceptance equipmentAcceptance = this.getById(id);
        if (equipmentAcceptance != null) {
            LambdaQueryWrapper<EquipmentAccessories> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(EquipmentAccessories::getEquipmentId, id);
            List<EquipmentAccessories> accessoriesList = accessoriesMapper.selectList(wrapper);
            equipmentAcceptance.setAccessoriesList(accessoriesList);
        }
        return equipmentAcceptance;
    }
}